package com.sneakxy.mybatis.commons.repository.query.domain.criterions;

import java.util.Iterator;
import java.util.List;

import org.apache.commons.lang3.RegExUtils;
import org.apache.commons.lang3.builder.EqualsBuilder;
import org.apache.commons.lang3.builder.HashCodeBuilder;
import org.apache.ibatis.scripting.xmltags.SqlNode;
import org.apache.ibatis.scripting.xmltags.StaticTextSqlNode;
import org.apache.ibatis.session.Configuration;

import com.sneakxy.mybatis.commons.dialect.Dialect;
import com.sneakxy.mybatis.commons.orm.mapping.MybatisColumn;

/**
 * @author 潜行的青衣
 */
public class EqCriterion extends AbstractCriterion {
	/**
	 * 
	 */
	private static final long serialVersionUID = -3526166834634745659L;
	
	private boolean ignoreCase;
	
	/**
	 * @param loader
	 * @param property
	 * @param value
	 */
	public EqCriterion(Class<?> loader, String property, Object value) {
		this(loader, property, value, false);
	}
	
	public EqCriterion(Class<?> loader, String property, Object value, boolean ignoreCase) {
		super(loader, property, value);
		this.ignoreCase = ignoreCase;
	}

	/* (non-Javadoc)
	 * @see com.sneakxy.mybatis.commons.repository.query.domain.Criterion#toSql(java.lang.String)
	 */
	@Override
	public SqlNode toSql(Configuration config, String operator, String parameterName) {
		Dialect dialect = this.getDialect(config);
		String remove = this.getRemove();
		List<MybatisColumn> columns = this.getColumns(config.getTypeHandlerRegistry(), parameterName);
		Iterator<MybatisColumn> it = columns.iterator();
		StringBuilder sqlBuilder = new StringBuilder();
		sqlBuilder.append(operator).append(" ").append(getOpen());
		MybatisColumn column = null;
		String columnValue = null;
		String columnName = null;
		while(it.hasNext()) {
			column = it.next();
			columnName = this.isIgnoreCase() ? dialect.upperFunction(dialect.formatColumnName(column.getColumn())) : dialect.formatColumnName(column.getColumn());
			columnValue = new StringBuilder().append("#{").append(RegExUtils.removeFirst(column.getPropertyPath(), remove)).append("}").toString();
			columnValue = this.isIgnoreCase() ? dialect.upperFunction(columnValue) : columnValue;
			sqlBuilder.append(columnName).append("=").append(columnValue);
			if(it.hasNext()) {
				sqlBuilder.append(" AND ");
			} else {
				sqlBuilder.append(getClose()).append(" ");
			}
		}
		return new StaticTextSqlNode(sqlBuilder.toString());
	}

	@Override
	public int hashCode() {
		return new HashCodeBuilder()
			.append(this.getValue())
			.append(this.getProperty())
			.append(this.getDomainClass())
			.append(this.ignoreCase).toHashCode();
	}

	@Override
	public boolean equals(Object obj) {
		if(obj == null) {
			return false;
		}
		if(!(obj instanceof EqCriterion)) {
			return false;
		}
		EqCriterion criterion = (EqCriterion) obj;
		return new EqualsBuilder()
				.append(this.getValue(), criterion.getValue())
				.append(this.getProperty(), criterion.getProperty())
				.append(this.getDomainClass(), criterion.getDomainClass())
				.append(this.ignoreCase, criterion.ignoreCase).isEquals();
	}
	
	public boolean isIgnoreCase() {
		return ignoreCase;
	}
	
}
